package com.hft.service.trans.impl;

import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hft.biz.Vo.UrlVo;
import com.hft.dal.mapper.UrlMapper;
import com.hft.dal.model.UrlDo;
import com.hft.dal.model.UrlExtDo;
import com.hft.service.convert.UrlConvert;
import com.hft.service.trans.UrlService;

@Service("urlService")
public class UrlServiceImpl implements UrlService {
    @Autowired
    UrlMapper urlMapper;

    @Override
    public int insertAndGetId(UrlVo urlVo) {
        UrlDo urlDo = new UrlDo();
        UrlConvert.convertVo2Do(urlVo, urlDo);
        int code = urlMapper.insertAndGetId(urlDo);
        if (code == 1) {
            urlVo.setUrlId(urlDo.getUrlId()); //insert成功返回主键
        }
        return code;
    }

    @Override
    public int updateByPK(UrlVo urlVo) {
        UrlDo urlDo = new UrlDo();
        UrlConvert.convertVo2Do(urlVo, urlDo);
        return urlMapper.updateByPK(urlDo);
    }

    @Override
    public UrlVo selectUrl(UrlVo urlVo) {
        UrlDo urlDo = new UrlDo();
        UrlConvert.convertVo2Do(urlVo, urlDo);
        urlDo = urlMapper.selectUrl(urlDo);
        if (null == urlDo) {
            return null;
        }
        UrlConvert.convertDo2Vo(urlDo, urlVo);
        return urlVo;
    }

    /** 
     * @see com.hft.service.trans.UrlService#selectByPage(com.hft.biz.vo.UrlVo, int, int)
     */
    @Override
    public List<UrlVo> selectByPage(UrlVo urlVo, int startRow, int num) {
        if (null == urlVo) {
            urlVo = new UrlVo();
        }
        UrlExtDo urlExtDo = new UrlExtDo();
        UrlConvert.convertVo2Do(urlVo, urlExtDo);
        urlExtDo.setStartRow(startRow);
        urlExtDo.setNum(num);
        List<UrlDo> urlDoList = new LinkedList<UrlDo>();
        List<UrlVo> urlVoList = new LinkedList<UrlVo>();
        urlDoList = urlMapper.selectByPage(urlExtDo);
        if (null == urlDoList || urlDoList.size() == 0) {
            return null;
        }
        UrlVo UrlVoTemp = null;
        for (UrlDo urlDo : urlDoList) {
            UrlVoTemp = new UrlVo();
            UrlConvert.convertDo2Vo(urlDo, UrlVoTemp);
            urlVoList.add(UrlVoTemp);
        }
        return urlVoList;
    }

    /** 
     * @see com.hft.service.trans.UrlService#selectAll()
     */
    @Override
    public List<UrlVo> selectAll() {
        List<UrlDo> urlDoList = new LinkedList<UrlDo>();
        List<UrlVo> urlVoList = new LinkedList<UrlVo>();
        urlDoList = urlMapper.selectAll();
        if (null == urlDoList || urlDoList.size() == 0) {
            return null;
        }
        UrlVo UrlVo = null;
        for (UrlDo urlDo : urlDoList) {
            UrlVo = new UrlVo();
            UrlConvert.convertDo2Vo(urlDo, UrlVo);
            urlVoList.add(UrlVo);
        }
        return urlVoList;
    }
}
